***************
* Loading data*
***************

*** Load the data


*********************
* Preparing the data*
*********************

* Variable: adopt_dm7, adopt_dm9, adopt_legal4, adopt_legal6
* Description: Whether the parliament adopted (ie. use it at t0, while didn't use it at t-1) low district magnitude (with alternative cut offs 7-9) or high electoral threshold (with alternative cut-offs 4-6) by year

by country (year), sort: gen byte adopt_dm7 = sum(dv_dm7 == 1) == 1  & sum(dv_dm7[_n - 1] == 1) == 0  
by country (year), sort: gen lag_dv_dm7 = dv_dm7[_n-1]
replace adopt_dm7=1 if lag_dv_dm7==0 & dv_dm7==1

by country (year), sort: gen byte adopt_dm9 = sum(dv_dm9 == 1) == 1  & sum(dv_dm9[_n - 1] == 1) == 0  
by country (year), sort: gen lag_dv_dm9 = dv_dm9[_n-1]
replace adopt_dm9=1 if lag_dv_dm9==0 & dv_dm9==1

by country (year), sort: gen byte adopt_legal4 = sum(dv_legal4 == 1) == 1  & sum(dv_legal4[_n - 1] == 1) == 0  
by country (year), sort: gen lag_dv_legal4 = dv_legal4[_n-1]
replace adopt_legal4=1 if lag_dv_legal4==0 & dv_legal4==1

by country (year), sort: gen byte adopt_legal6 = sum(dv_legal6 == 1) == 1  & sum(dv_legal6[_n - 1] == 1) == 0  
by country (year), sort: gen lag_dv_legal6 = dv_legal6[_n-1]
replace adopt_legal6=1 if lag_dv_legal6==0 & dv_legal6==1

replace adopt_dm7=0 if country=="Belgium" & year==1946
replace adopt_dm7=0 if country=="Denmark" & year==1947
replace adopt_dm7=0 if country=="Finland" & year==1948
replace adopt_dm7=0 if country=="Luxembourg" & year==1945
replace adopt_dm7=0 if country=="Netherlands" & year==1946
replace adopt_dm7=0 if country=="Sweden" & year==1948
replace adopt_dm7=0 if country=="Switzerland" & year==1948

replace adopt_dm9=0 if country=="Belgium" & year==1946
replace adopt_dm9=0 if country=="Denmark" & year==1947
replace adopt_dm9=0 if country=="Finland" & year==1948
replace adopt_dm9=0 if country=="Luxembourg" & year==1945
replace adopt_dm9=0 if country=="Netherlands" & year==1946
replace adopt_dm9=0 if country=="Sweden" & year==1948
replace adopt_dm9=0 if country=="Switzerland" & year==1948

replace adopt_legal4=0 if country=="Belgium" & year==1946
replace adopt_legal4=0 if country=="Denmark" & year==1947
replace adopt_legal4=0 if country=="Finland" & year==1948
replace adopt_legal4=0 if country=="Luxembourg" & year==1945
replace adopt_legal4=0 if country=="Netherlands" & year==1946
replace adopt_legal4=0 if country=="Sweden" & year==1948
replace adopt_legal4=0 if country=="Switzerland" & year==1948

replace adopt_legal6=0 if country=="Belgium" & year==1946
replace adopt_legal6=0 if country=="Denmark" & year==1947
replace adopt_legal6=0 if country=="Finland" & year==1948
replace adopt_legal6=0 if country=="Luxembourg" & year==1945
replace adopt_legal6=0 if country=="Netherlands" & year==1946
replace adopt_legal6=0 if country=="Sweden" & year==1948
replace adopt_legal6=0 if country=="Switzerland" & year==1948

gen number_dm7=.
replace number_dm7=0 if year<=1994 & country=="Austria"
replace number_dm7=1 if year>=1985 & country=="Belgium"
replace number_dm7=1 if year>=1991 & country=="Bulgaria"
replace number_dm7=0 if country=="Croatia"
replace number_dm7=1 if year>=1985 & country=="Cyprus"
replace number_dm7=1 if country=="Czech Republic"
replace number_dm7=1 if year>=1971 & country=="Denmark"
replace number_dm7=0 if country=="Estonia"
replace number_dm7=0 if country=="Finland"
replace number_dm7=0 if country=="Germany"
replace number_dm7=0 if country=="Italy" & year<=1994
replace number_dm7=1 if country=="Italy" & year>=2006
replace number_dm7=0 if country=="Latvia"
replace number_dm7=0 if country=="Luxembourg"
replace number_dm7=0 if country=="Netherlands"
replace number_dm7=1 if year>=1973 & country=="Norway"
replace number_dm7=0 if year<=1993 & country=="Poland"
replace number_dm7=0 if country=="Portugal"
replace number_dm7=0 if country=="Romania"
replace number_dm7=0 if country=="Slovakia"
replace number_dm7=0 if country=="Slovenia"
replace number_dm7=0 if country=="Sweden"
replace number_dm7=0 if country=="Switzerland"


gen number_dm9=.
replace number_dm9=1 if year>=1970 & year<=1994 & country=="Austria"
replace number_dm9=1 if year>=1985 & country=="Belgium"
replace number_dm9=0 if country=="Croatia"
replace number_dm9=1 if year>=1985 & country=="Cyprus"
replace number_dm9=0 if country=="Czech Republic"
replace number_dm9=0 if country=="Finland"
replace number_dm9=0 if country=="Germany"
replace number_dm9=0 if country=="Italy" & year<=1994
replace number_dm9=1 if country=="Italy" & year>=2006
replace number_dm9=0 if country=="Latvia"
replace number_dm9=0 if country=="Luxembourg"
replace number_dm9=0 if country=="Netherlands"
replace number_dm9=1 if year>=1973 & country=="Norway"
replace number_dm9=0 if year<=1993 & country=="Poland"
replace number_dm9=0 if country=="Portugal"
replace number_dm9=0 if year<=1992 & country=="Romania"
replace number_dm9=0 if country=="Slovakia"
replace number_dm9=0 if country=="Slovenia"
replace number_dm9=0 if country=="Sweden"
replace number_dm9=0 if country=="Switzerland"



gen number_legal4=.
replace number_legal4=0 if year<=1994 & country=="Austria"
replace number_legal4=0 if year<=2003 & country=="Belgium"
replace number_legal4=0 if year<=1995 & country=="Croatia"
replace number_legal4=1 if year>=1996 & country=="Cyprus"
replace number_legal4=0 if country=="Denmark"
replace number_legal4=0 if country=="Finland"
replace number_legal4=1 if year>=1986 & country=="Greece"
replace number_legal4=0 if year<=2003 & country=="Iceland"
replace number_legal4=0 if year<=1994 & country=="Italy"
replace number_legal4=0 if country=="Luxembourg"
replace number_legal4=0 if country=="Netherlands"
replace number_legal4=0 if year<=1989 & country=="Norway"
replace number_legal4=0 if year<=1993 & country=="Poland"
replace number_legal4=0 if country=="Portugal"
replace number_legal4=0 if year<=2000 & country=="Romania"
replace number_legal4=0 if year<=2000 & country=="Slovenia"
replace number_legal4=0 if country=="Spain"
replace number_legal4=0 if year<=1970 & country=="Sweden"
replace number_legal4=0 if country=="Switzerland"

gen number_legal6=.
replace number_legal6=0 if country=="Austria"
replace number_legal6=0 if country=="Belgium"
replace number_legal6=0 if country=="Bulgaria"
replace number_legal6=0 if country=="Croatia"
replace number_legal6=1 if year>=1996 & country=="Cyprus"
replace number_legal6=0 if country=="Czech Republic"
replace number_legal6=0 if country=="Denmark"
replace number_legal6=0 if country=="Estonia"
replace number_legal6=0 if country=="Finland"
replace number_legal6=0 if country=="Germany"
replace number_legal6=1 if year>=1986 & country=="Greece"
replace number_legal6=0 if country=="Hungary"
replace number_legal6=0 if country=="Iceland"
replace number_legal6=0 if country=="Italy"
replace number_legal6=0 if country=="Latvia"
replace number_legal6=0 if country=="Lithuania"
replace number_legal6=0 if country=="Luxembourg"
replace number_legal6=0 if country=="Netherlands"
replace number_legal6=0 if country=="Norway"
replace number_legal6=0 if country=="Poland"
replace number_legal6=0 if country=="Portugal"
replace number_legal6=0 if country=="Romania"
replace number_legal6=0 if country=="Slovenia"
replace number_legal6=0 if country=="Sweden"
replace number_legal6=0 if country=="Switzerland"

* Variables: group_ling, group_demo, group_bound_[country]
* Description: Groups of linguistic, historical and geographical peers to which the democracy belongs

gen group_ling=.
replace group_ling=1 if country=="Italy"
replace group_ling=1 if country=="Portugal"
replace group_ling=1 if country=="Romania"
replace group_ling=1 if country=="Spain"
replace group_ling=2 if country=="Germany" 
replace group_ling=2 if country=="Austria" 
replace group_ling=2 if country=="Switzerland"
replace group_ling=2 if country=="Norway"
replace group_ling=2 if country=="Sweden"
replace group_ling=2 if country=="Iceland"
replace group_ling=2 if country=="Netherlands"
replace group_ling=2 if country=="Belgium"
replace group_ling=2 if country=="Luxembourg"
replace group_ling=2 if country=="Denmark"
replace group_ling=3 if country=="Czech Republic"
replace group_ling=3 if country=="Slovakia"
replace group_ling=3 if country=="Poland"
replace group_ling=3 if country=="Slovenia"
replace group_ling=3 if country=="Croatia"
replace group_ling=3 if country=="Bulgaria"
replace group_ling=3 if country=="Latvia"
replace group_ling=3 if country=="Lithuania" 
replace group_ling=4 if country=="Greece"
replace group_ling=4 if country=="Cyprus"
replace group_ling=5 if country=="Hungary"
replace group_ling=5 if country=="Finland"
replace group_ling=5 if country=="Estonia"

gen group_demo=.
replace group_demo=1 if country=="Czech Republic"
replace group_demo=1 if country=="Slovakia"
replace group_demo=1 if country=="Poland"
replace group_demo=1 if country=="Slovenia"
replace group_demo=1 if country=="Hungary"
replace group_demo=1 if country=="Croatia"
replace group_demo=1 if country=="Bulgaria"
replace group_demo=1 if country=="Latvia"
replace group_demo=1 if country=="Lithuania"
replace group_demo=1 if country=="Estonia"
replace group_demo=1 if country=="Romania"
replace group_demo=2 if country=="Cyprus"
replace group_demo=2 if country=="Greece"
replace group_demo=2 if country=="Portugal"
replace group_demo=2 if country=="Spain"
replace group_demo=3 if country=="Austria"
replace group_demo=3 if country=="Switzerland"
replace group_demo=3 if country=="Norway"
replace group_demo=3 if country=="Sweden"
replace group_demo=3 if country=="Iceland"
replace group_demo=3 if country=="Netherlands"
replace group_demo=3 if country=="Belgium"
replace group_demo=3 if country=="Luxembourg"
replace group_demo=3 if country=="Denmark"
replace group_demo=3 if country=="Finland"
replace group_demo=3 if country=="Germany"
replace group_demo=3 if country=="Italy"

gen group_bound_aus=.
replace group_bound_aus=1 if country=="Austria"
replace group_bound_aus=1 if country=="Czech Republic"
replace group_bound_aus=1 if country=="Germany"
replace group_bound_aus=1 if country=="Hungary"
replace group_bound_aus=1 if country=="Italy"
replace group_bound_aus=1 if country=="Slovakia"
replace group_bound_aus=1 if country=="Slovenia"
replace group_bound_aus=1 if country=="Switzerland"

gen group_bound_bel=.
replace group_bound_bel=1 if country=="Belgium"
replace group_bound_bel=1 if country=="Germany"
replace group_bound_bel=1 if country=="Netherlands"
replace group_bound_bel=1 if country=="Luxembourg"

gen group_bound_bul=.
replace group_bound_bul=1 if country=="Bulgaria"
replace group_bound_bul=1 if country=="Greece"
replace group_bound_bul=1 if country=="Romania"

gen group_bound_cro=.
replace group_bound_cro=1 if country=="Croatia"
replace group_bound_cro=1 if country=="Hungary"
replace group_bound_cro=1 if country=="Slovenia"
replace group_bound_cro=1 if country=="Italy"

gen group_bound_cyp=.
replace group_bound_cyp=1 if country=="Cyprus"
replace group_bound_cyp=1 if country=="Greece"

gen group_bound_cze=.
replace group_bound_cze=1 if country=="Czech Republic"
replace group_bound_cze=1 if country=="Slovakia"
replace group_bound_cze=1 if country=="Poland"
replace group_bound_cze=1 if country=="Hungary"
replace group_bound_cze=1 if country=="Germany"

gen group_bound_den=.
replace group_bound_den=1 if country=="Denmark"
replace group_bound_den=1 if country=="Germany"
replace group_bound_den=1 if country=="Norway"
replace group_bound_den=1 if country=="Sweden"
replace group_bound_den=1 if country=="Poland"

gen group_bound_est=.
replace group_bound_est=1 if country=="Estonia"
replace group_bound_est=1 if country=="Finland"
replace group_bound_est=1 if country=="Latvia"
replace group_bound_est=1 if country=="Sweden"

gen group_bound_fin=.
replace group_bound_fin=1 if country=="Finland"
replace group_bound_fin=1 if country=="Sweden"
replace group_bound_fin=1 if country=="Norway"
replace group_bound_fin=1 if country=="Estonia"

gen group_bound_ger=.
replace group_bound_ger=1 if country=="Germany"
replace group_bound_ger=1 if country=="Netherlands"
replace group_bound_ger=1 if country=="Belgium"
replace group_bound_ger=1 if country=="Luxembourg"
replace group_bound_ger=1 if country=="Czech Republic"
replace group_bound_ger=1 if country=="Austria"
replace group_bound_ger=1 if country=="Denmark"
replace group_bound_ger=1 if country=="Poland"
replace group_bound_ger=1 if country=="Switzerland"
replace group_bound_ger=1 if country=="Sweden"

gen group_bound_gre=.
replace group_bound_gre=1 if country=="Greece"
replace group_bound_gre=1 if country=="Bulgaria"
replace group_bound_gre=1 if country=="Cyprus"
replace group_bound_gre=1 if country=="Italy"

gen group_bound_hun=.
replace group_bound_hun=1 if country=="Hungary"
replace group_bound_hun=1 if country=="Romania"
replace group_bound_hun=1 if country=="Slovakia"
replace group_bound_hun=1 if country=="Austria"
replace group_bound_hun=1 if country=="Croatia"
replace group_bound_hun=1 if country=="Slovenia"

gen group_bound_ice=.

gen group_bound_ita=.
replace group_bound_ita=1 if country=="Italy"
replace group_bound_ita=1 if country=="Switzerland"
replace group_bound_ita=1 if country=="Slovenia"
replace group_bound_ita=1 if country=="Austria"
replace group_bound_ita=1 if country=="Greece"
replace group_bound_ita=1 if country=="Spain"
replace group_bound_ita=1 if country=="Croatia"

gen group_bound_lat=.
replace group_bound_lat=1 if country=="Latvia"
replace group_bound_lat=1 if country=="Estonia"
replace group_bound_lat=1 if country=="Lithuania"
replace group_bound_lat=1 if country=="Sweden"

gen group_bound_lit=.
replace group_bound_lit=1 if country=="Lithuania"
replace group_bound_lit=1 if country=="Latvia"
replace group_bound_lit=1 if country=="Poland"
replace group_bound_lit=1 if country=="Sweden"

gen group_bound_lux=.
replace group_bound_lux=1 if country=="Luxembourg"
replace group_bound_lux=1 if country=="Belgium"
replace group_bound_lux=1 if country=="Germany"

gen group_bound_net=.
replace group_bound_net=1 if country=="Netherlands"
replace group_bound_net=1 if country=="Germany"
replace group_bound_net=1 if country=="Belgium"

gen group_bound_nor=.
replace group_bound_nor=1 if country=="Norway"
replace group_bound_nor=1 if country=="Sweden"
replace group_bound_nor=1 if country=="Iceland"
replace group_bound_nor=1 if country=="Finland"

gen group_bound_pol=.
replace group_bound_pol=1 if country=="Poland"
replace group_bound_pol=1 if country=="Germany"
replace group_bound_pol=1 if country=="Czech Republic"
replace group_bound_pol=1 if country=="Slovakia"
replace group_bound_pol=1 if country=="Lithuania"
replace group_bound_pol=1 if country=="Sweden"
replace group_bound_pol=1 if country=="Denmark"

gen group_bound_por=.
replace group_bound_por=1 if country=="Portugal"
replace group_bound_por=1 if country=="Spain"

gen group_bound_rom=.
replace group_bound_rom=1 if country=="Romania"
replace group_bound_rom=1 if country=="Bulgaria"
replace group_bound_rom=1 if country=="Hungary"

gen group_bound_slo=.
replace group_bound_slo=1 if country=="Slovakia"
replace group_bound_slo=1 if country=="Czech Republic"
replace group_bound_slo=1 if country=="Austria"
replace group_bound_slo=1 if country=="Hungary"
replace group_bound_slo=1 if country=="Poland"

gen group_bound_slove=.
replace group_bound_slove=1 if country=="Slovenia"
replace group_bound_slove=1 if country=="Croatia"
replace group_bound_slove=1 if country=="Italy"
replace group_bound_slove=1 if country=="Austria"
replace group_bound_slove=1 if country=="Hungary"

gen group_bound_spa=.
replace group_bound_spa=1 if country=="Spain"
replace group_bound_spa=1 if country=="Portugal"
replace group_bound_spa=1 if country=="Italy"

gen group_bound_swe=.
replace group_bound_swe=1 if country=="Sweden"
replace group_bound_swe=1 if country=="Norway"
replace group_bound_swe=1 if country=="Denmark"
replace group_bound_swe=1 if country=="Finland"
replace group_bound_swe=1 if country=="Germany"
replace group_bound_swe=1 if country=="Latvia"
replace group_bound_swe=1 if country=="Lithuania"
replace group_bound_swe=1 if country=="Poland"
replace group_bound_swe=1 if country=="Estonia"

gen group_bound_swi=.
replace group_bound_swi=1 if country=="Switzerland"
replace group_bound_swi=1 if country=="Germany"
replace group_bound_swi=1 if country=="Austria"
replace group_bound_swi=1 if country=="Italy"

* Variables: n_peers_ling, n_peers_demo, n_peers_bound
* Description: Number of democracies in each of the linguistic, historical and geographical group of peers by year

gen n=1

bysort year group_ling: egen n_peers_ling = total(n)
replace n_peers_ling=n_peers_ling-1

bysort year group_demo: egen n_peers_demo = total(n)
replace n_peers_demo=n_peers_demo-1

bysort year group_bound_aus: egen n_peers_bound_aus = total(n) if group_bound_aus==1
replace n_peers_bound_aus = n_peers_bound_aus-1
bysort year group_bound_bel: egen n_peers_bound_bel = total(n) if group_bound_bel==1
replace n_peers_bound_bel = n_peers_bound_bel-1
bysort year group_bound_bul: egen n_peers_bound_bul = total(n) if group_bound_bul==1
replace n_peers_bound_bul = n_peers_bound_bul-1
bysort year group_bound_cro: egen n_peers_bound_cro = total(n) if group_bound_cro==1
replace n_peers_bound_cro = n_peers_bound_cro-1
bysort year group_bound_cyp: egen n_peers_bound_cyp = total(n) if group_bound_cyp==1
replace n_peers_bound_cyp = n_peers_bound_cyp-1
bysort year group_bound_cze: egen n_peers_bound_cze = total(n) if group_bound_cze==1
replace n_peers_bound_cze = n_peers_bound_cze-1
bysort year group_bound_den: egen n_peers_bound_den = total(n) if group_bound_den==1
replace n_peers_bound_den = n_peers_bound_den-1
bysort year group_bound_est: egen n_peers_bound_est = total(n) if group_bound_est==1
replace n_peers_bound_est = n_peers_bound_est-1
bysort year group_bound_fin: egen n_peers_bound_fin = total(n) if group_bound_fin==1
replace n_peers_bound_fin = n_peers_bound_fin-1
bysort year group_bound_ger: egen n_peers_bound_ger = total(n) if group_bound_ger==1
replace n_peers_bound_ger = n_peers_bound_ger-1
bysort year group_bound_gre: egen n_peers_bound_gre = total(n) if group_bound_gre==1
replace n_peers_bound_gre = n_peers_bound_gre-1
bysort year group_bound_hun: egen n_peers_bound_hun = total(n) if group_bound_hun==1
replace n_peers_bound_hun = n_peers_bound_hun-1
bysort year group_bound_ice: egen n_peers_bound_ice = total(n) if group_bound_ice==1
replace n_peers_bound_ice = n_peers_bound_ice-1
bysort year group_bound_ita: egen n_peers_bound_ita = total(n) if group_bound_ita==1
replace n_peers_bound_ita = n_peers_bound_ita-1
bysort year group_bound_lat: egen n_peers_bound_lat = total(n) if group_bound_lat==1
replace n_peers_bound_lat = n_peers_bound_lat-1
bysort year group_bound_lit: egen n_peers_bound_lit = total(n) if group_bound_lit==1
replace n_peers_bound_lit = n_peers_bound_lit-1
bysort year group_bound_lux: egen n_peers_bound_lux = total(n) if group_bound_lux==1
replace n_peers_bound_lux = n_peers_bound_lux-1
bysort year group_bound_net: egen n_peers_bound_net = total(n) if group_bound_net==1
replace n_peers_bound_net = n_peers_bound_net-1
bysort year group_bound_nor: egen n_peers_bound_nor = total(n) if group_bound_nor==1
replace n_peers_bound_nor = n_peers_bound_nor-1
bysort year group_bound_pol: egen n_peers_bound_pol = total(n) if group_bound_pol==1
replace n_peers_bound_pol = n_peers_bound_pol-1
bysort year group_bound_por: egen n_peers_bound_por = total(n) if group_bound_por==1
replace n_peers_bound_por = n_peers_bound_por-1
bysort year group_bound_rom: egen n_peers_bound_rom = total(n) if group_bound_rom==1
replace n_peers_bound_rom = n_peers_bound_rom-1
bysort year group_bound_slo: egen n_peers_bound_slo = total(n) if group_bound_slo==1
replace n_peers_bound_slo = n_peers_bound_slo-1
bysort year group_bound_slove: egen n_peers_bound_slove = total(n) if group_bound_slove==1
replace n_peers_bound_slove = n_peers_bound_slove-1
bysort year group_bound_spa: egen n_peers_bound_spa = total(n) if group_bound_spa==1
replace n_peers_bound_spa = n_peers_bound_spa-1
bysort year group_bound_swe: egen n_peers_bound_swe = total(n) if group_bound_swe==1
replace n_peers_bound_swe = n_peers_bound_swe-1
bysort year group_bound_swi: egen n_peers_bound_swi = total(n) if group_bound_swi==1
replace n_peers_bound_swi = n_peers_bound_swi

gen n_peers_bound=.
replace n_peers_bound=n_peers_bound_aus if country=="Austria"
replace n_peers_bound=n_peers_bound_bel if country=="Belgium"
replace n_peers_bound=n_peers_bound_bul if country=="Bulgaria"
replace n_peers_bound=n_peers_bound_cro if country=="Croatia"
replace n_peers_bound=n_peers_bound_cyp if country=="Cyprus"
replace n_peers_bound=n_peers_bound_cze if country=="Czech Republic"
replace n_peers_bound=n_peers_bound_den if country=="Denmark"
replace n_peers_bound=n_peers_bound_est if country=="Estonia"
replace n_peers_bound=n_peers_bound_fin if country=="Finland"
replace n_peers_bound=n_peers_bound_ger if country=="Germany"
replace n_peers_bound=n_peers_bound_gre if country=="Greece"
replace n_peers_bound=n_peers_bound_hun if country=="Hungary"
replace n_peers_bound=n_peers_bound_ice if country=="Iceland"
replace n_peers_bound=n_peers_bound_ita if country=="Italy"
replace n_peers_bound=n_peers_bound_lat if country=="Latvia"
replace n_peers_bound=n_peers_bound_lit if country=="Lithuania"
replace n_peers_bound=n_peers_bound_lux if country=="Luxembourg"
replace n_peers_bound=n_peers_bound_net if country=="Netherlands"
replace n_peers_bound=n_peers_bound_nor if country=="Norway"
replace n_peers_bound=n_peers_bound_pol if country=="Poland"
replace n_peers_bound=n_peers_bound_por if country=="Portugal"
replace n_peers_bound=n_peers_bound_rom if country=="Romania"
replace n_peers_bound=n_peers_bound_slo if country=="Slovakia"
replace n_peers_bound=n_peers_bound_slove if country=="Slovenia"
replace n_peers_bound=n_peers_bound_spa if country=="Spain"
replace n_peers_bound=n_peers_bound_swe if country=="Sweden"
replace n_peers_bound=n_peers_bound_swi if country=="Switzerland"

* Variables: dm7_ling, dm7_demo, dm7_bound, dm9_ling, dm9_demo, dm9_bound, legal4_ling, legal4_demo, legal4_bound, legal6_ling, legal6_demo, legal6_bound,
* Description: Proportion of linguistic, historical and geographical peers that have adopted ow district magnitude (with alternative cut offs 7-9) or high electoral threshold (with alternative cut-offs 4-6) by year

bysort year group_ling: egen dm7_ling=total(adopt_dm7)
bysort year group_demo: egen dm7_demo=total(adopt_dm7)
bysort year group_bound_aus: egen dm7_bound_aus= total(adopt_dm7)
bysort year group_bound_bel: egen dm7_bound_bel= total(adopt_dm7)
bysort year group_bound_bul: egen dm7_bound_bul= total(adopt_dm7)
bysort year group_bound_cro: egen dm7_bound_cro= total(adopt_dm7)
bysort year group_bound_cyp: egen dm7_bound_cyp= total(adopt_dm7)
bysort year group_bound_cze: egen dm7_bound_cze= total(adopt_dm7)
bysort year group_bound_den: egen dm7_bound_den= total(adopt_dm7)
bysort year group_bound_est: egen dm7_bound_est= total(adopt_dm7)
bysort year group_bound_fin: egen dm7_bound_fin= total(adopt_dm7)
bysort year group_bound_ger: egen dm7_bound_ger= total(adopt_dm7)
bysort year group_bound_gre: egen dm7_bound_gre= total(adopt_dm7)
bysort year group_bound_hun: egen dm7_bound_hun= total(adopt_dm7)
bysort year group_bound_ice: egen dm7_bound_ice= total(adopt_dm7)
bysort year group_bound_ita: egen dm7_bound_ita= total(adopt_dm7)
bysort year group_bound_lat: egen dm7_bound_lat= total(adopt_dm7)
bysort year group_bound_lit: egen dm7_bound_lit= total(adopt_dm7)
bysort year group_bound_lux: egen dm7_bound_lux= total(adopt_dm7)
bysort year group_bound_net: egen dm7_bound_net= total(adopt_dm7)
bysort year group_bound_nor: egen dm7_bound_nor= total(adopt_dm7)
bysort year group_bound_pol: egen dm7_bound_pol= total(adopt_dm7)
bysort year group_bound_por: egen dm7_bound_por= total(adopt_dm7)
bysort year group_bound_rom: egen dm7_bound_rom= total(adopt_dm7)
bysort year group_bound_slo: egen dm7_bound_slo= total(adopt_dm7)
bysort year group_bound_slove: egen dm7_bound_slove= total(adopt_dm7)
bysort year group_bound_spa: egen dm7_bound_spa= total(adopt_dm7)
bysort year group_bound_swe: egen dm7_bound_swe= total(adopt_dm7)
bysort year group_bound_swi: egen dm7_bound_swi= total(adopt_dm7)

gen dm7_bound=.
replace dm7_bound=dm7_bound_aus if country=="Austria"
replace dm7_bound=dm7_bound_bel if country=="Belgium"
replace dm7_bound=dm7_bound_bul if country=="Bulgaria"
replace dm7_bound=dm7_bound_cro if country=="Croatia"
replace dm7_bound=dm7_bound_cyp if country=="Cyprus"
replace dm7_bound=dm7_bound_cze if country=="Czech Republic"
replace dm7_bound=dm7_bound_den if country=="Denmark"
replace dm7_bound=dm7_bound_est if country=="Estonia"
replace dm7_bound=dm7_bound_fin if country=="Finland"
replace dm7_bound=dm7_bound_ger if country=="Germany"
replace dm7_bound=dm7_bound_gre if country=="Greece"
replace dm7_bound=dm7_bound_hun if country=="Hungary"
replace dm7_bound=dm7_bound_ice if country=="Iceland"
replace dm7_bound=dm7_bound_ita if country=="Italy"
replace dm7_bound=dm7_bound_lat if country=="Latvia"
replace dm7_bound=dm7_bound_lit if country=="Lithuania"
replace dm7_bound=dm7_bound_lux if country=="Luxembourg"
replace dm7_bound=dm7_bound_net if country=="Netherlands"
replace dm7_bound=dm7_bound_nor if country=="Norway"
replace dm7_bound=dm7_bound_pol if country=="Poland"
replace dm7_bound=dm7_bound_por if country=="Portugal"
replace dm7_bound=dm7_bound_rom if country=="Romania"
replace dm7_bound=dm7_bound_slo if country=="Slovakia"
replace dm7_bound=dm7_bound_slove if country=="Slovenia"
replace dm7_bound=dm7_bound_spa if country=="Spain"
replace dm7_bound=dm7_bound_swe if country=="Sweden"
replace dm7_bound=dm7_bound_swi if country=="Switzerland"

replace dm7_ling=dm7_ling-1 if adopt_dm7==1
replace dm7_demo=dm7_demo-1 if adopt_dm7==1
replace dm7_bound=dm7_bound-1 if adopt_dm7==1

replace dm7_ling=dm7_ling/n_peers_ling
replace dm7_demo=dm7_demo/n_peers_demo
replace dm7_bound=dm7_bound/n_peers_bound

bysort year group_ling: egen dm9_ling=total(adopt_dm9)
bysort year group_demo: egen dm9_demo=total(adopt_dm9)
bysort year group_bound_aus: egen dm9_bound_aus= total(adopt_dm9)
bysort year group_bound_bel: egen dm9_bound_bel= total(adopt_dm9)
bysort year group_bound_bul: egen dm9_bound_bul= total(adopt_dm9)
bysort year group_bound_cro: egen dm9_bound_cro= total(adopt_dm9)
bysort year group_bound_cyp: egen dm9_bound_cyp= total(adopt_dm9)
bysort year group_bound_cze: egen dm9_bound_cze= total(adopt_dm9)
bysort year group_bound_den: egen dm9_bound_den= total(adopt_dm9)
bysort year group_bound_est: egen dm9_bound_est= total(adopt_dm9)
bysort year group_bound_fin: egen dm9_bound_fin= total(adopt_dm9)
bysort year group_bound_ger: egen dm9_bound_ger= total(adopt_dm9)
bysort year group_bound_gre: egen dm9_bound_gre= total(adopt_dm9)
bysort year group_bound_hun: egen dm9_bound_hun= total(adopt_dm9)
bysort year group_bound_ice: egen dm9_bound_ice= total(adopt_dm9)
bysort year group_bound_ita: egen dm9_bound_ita= total(adopt_dm9)
bysort year group_bound_lat: egen dm9_bound_lat= total(adopt_dm9)
bysort year group_bound_lit: egen dm9_bound_lit= total(adopt_dm9)
bysort year group_bound_lux: egen dm9_bound_lux= total(adopt_dm9)
bysort year group_bound_net: egen dm9_bound_net= total(adopt_dm9)
bysort year group_bound_nor: egen dm9_bound_nor= total(adopt_dm9)
bysort year group_bound_pol: egen dm9_bound_pol= total(adopt_dm9)
bysort year group_bound_por: egen dm9_bound_por= total(adopt_dm9)
bysort year group_bound_rom: egen dm9_bound_rom= total(adopt_dm9)
bysort year group_bound_slo: egen dm9_bound_slo= total(adopt_dm9)
bysort year group_bound_slove: egen dm9_bound_slove= total(adopt_dm9)
bysort year group_bound_spa: egen dm9_bound_spa= total(adopt_dm9)
bysort year group_bound_swe: egen dm9_bound_swe= total(adopt_dm9)
bysort year group_bound_swi: egen dm9_bound_swi= total(adopt_dm9)

gen dm9_bound=.
replace dm9_bound=dm9_bound_aus if country=="Austria"
replace dm9_bound=dm9_bound_bel if country=="Belgium"
replace dm9_bound=dm9_bound_bul if country=="Bulgaria"
replace dm9_bound=dm9_bound_cro if country=="Croatia"
replace dm9_bound=dm9_bound_cyp if country=="Cyprus"
replace dm9_bound=dm9_bound_cze if country=="Czech Republic"
replace dm9_bound=dm9_bound_den if country=="Denmark"
replace dm9_bound=dm9_bound_est if country=="Estonia"
replace dm9_bound=dm9_bound_fin if country=="Finland"
replace dm9_bound=dm9_bound_ger if country=="Germany"
replace dm9_bound=dm9_bound_gre if country=="Greece"
replace dm9_bound=dm9_bound_hun if country=="Hungary"
replace dm9_bound=dm9_bound_ice if country=="Iceland"
replace dm9_bound=dm9_bound_ita if country=="Italy"
replace dm9_bound=dm9_bound_lat if country=="Latvia"
replace dm9_bound=dm9_bound_lit if country=="Lithuania"
replace dm9_bound=dm9_bound_lux if country=="Luxembourg"
replace dm9_bound=dm9_bound_net if country=="Netherlands"
replace dm9_bound=dm9_bound_nor if country=="Norway"
replace dm9_bound=dm9_bound_pol if country=="Poland"
replace dm9_bound=dm9_bound_por if country=="Portugal"
replace dm9_bound=dm9_bound_rom if country=="Romania"
replace dm9_bound=dm9_bound_slo if country=="Slovakia"
replace dm9_bound=dm9_bound_slove if country=="Slovenia"
replace dm9_bound=dm9_bound_spa if country=="Spain"
replace dm9_bound=dm9_bound_swe if country=="Sweden"
replace dm9_bound=dm9_bound_swi if country=="Switzerland"

replace dm9_ling=dm9_ling-1 if adopt_dm9==1
replace dm9_demo=dm9_demo-1 if adopt_dm9==1
replace dm9_bound=dm9_bound-1 if adopt_dm9==1

replace dm9_ling=dm9_ling/n_peers_ling
replace dm9_demo=dm9_demo/n_peers_demo
replace dm9_bound=dm9_bound/n_peers_bound

bysort year group_ling: egen legal4_ling=total(adopt_legal4)
bysort year group_demo: egen legal4_demo=total(adopt_legal4)
bysort year group_bound_aus: egen legal4_bound_aus= total(adopt_legal4)
bysort year group_bound_bel: egen legal4_bound_bel= total(adopt_legal4)
bysort year group_bound_bul: egen legal4_bound_bul= total(adopt_legal4)
bysort year group_bound_cro: egen legal4_bound_cro= total(adopt_legal4)
bysort year group_bound_cyp: egen legal4_bound_cyp= total(adopt_legal4)
bysort year group_bound_cze: egen legal4_bound_cze= total(adopt_legal4)
bysort year group_bound_den: egen legal4_bound_den= total(adopt_legal4)
bysort year group_bound_est: egen legal4_bound_est= total(adopt_legal4)
bysort year group_bound_fin: egen legal4_bound_fin= total(adopt_legal4)
bysort year group_bound_ger: egen legal4_bound_ger= total(adopt_legal4)
bysort year group_bound_gre: egen legal4_bound_gre= total(adopt_legal4)
bysort year group_bound_hun: egen legal4_bound_hun= total(adopt_legal4)
bysort year group_bound_ice: egen legal4_bound_ice= total(adopt_legal4)
bysort year group_bound_ita: egen legal4_bound_ita= total(adopt_legal4)
bysort year group_bound_lat: egen legal4_bound_lat= total(adopt_legal4)
bysort year group_bound_lit: egen legal4_bound_lit= total(adopt_legal4)
bysort year group_bound_lux: egen legal4_bound_lux= total(adopt_legal4)
bysort year group_bound_net: egen legal4_bound_net= total(adopt_legal4)
bysort year group_bound_nor: egen legal4_bound_nor= total(adopt_legal4)
bysort year group_bound_pol: egen legal4_bound_pol= total(adopt_legal4)
bysort year group_bound_por: egen legal4_bound_por= total(adopt_legal4)
bysort year group_bound_rom: egen legal4_bound_rom= total(adopt_legal4)
bysort year group_bound_slo: egen legal4_bound_slo= total(adopt_legal4)
bysort year group_bound_slove: egen legal4_bound_slove= total(adopt_legal4)
bysort year group_bound_spa: egen legal4_bound_spa= total(adopt_legal4)
bysort year group_bound_swe: egen legal4_bound_swe= total(adopt_legal4)
bysort year group_bound_swi: egen legal4_bound_swi= total(adopt_legal4)

gen legal4_bound=.
replace legal4_bound=legal4_bound_aus if country=="Austria"
replace legal4_bound=legal4_bound_bel if country=="Belgium"
replace legal4_bound=legal4_bound_bul if country=="Bulgaria"
replace legal4_bound=legal4_bound_cro if country=="Croatia"
replace legal4_bound=legal4_bound_cyp if country=="Cyprus"
replace legal4_bound=legal4_bound_cze if country=="Czech Republic"
replace legal4_bound=legal4_bound_den if country=="Denmark"
replace legal4_bound=legal4_bound_est if country=="Estonia"
replace legal4_bound=legal4_bound_fin if country=="Finland"
replace legal4_bound=legal4_bound_ger if country=="Germany"
replace legal4_bound=legal4_bound_gre if country=="Greece"
replace legal4_bound=legal4_bound_hun if country=="Hungary"
replace legal4_bound=legal4_bound_ice if country=="Iceland"
replace legal4_bound=legal4_bound_ita if country=="Italy"
replace legal4_bound=legal4_bound_lat if country=="Latvia"
replace legal4_bound=legal4_bound_lit if country=="Lithuania"
replace legal4_bound=legal4_bound_lux if country=="Luxembourg"
replace legal4_bound=legal4_bound_net if country=="Netherlands"
replace legal4_bound=legal4_bound_nor if country=="Norway"
replace legal4_bound=legal4_bound_pol if country=="Poland"
replace legal4_bound=legal4_bound_por if country=="Portugal"
replace legal4_bound=legal4_bound_rom if country=="Romania"
replace legal4_bound=legal4_bound_slo if country=="Slovakia"
replace legal4_bound=legal4_bound_slove if country=="Slovenia"
replace legal4_bound=legal4_bound_spa if country=="Spain"
replace legal4_bound=legal4_bound_swe if country=="Sweden"
replace legal4_bound=legal4_bound_swi if country=="Switzerland"

replace legal4_ling=legal4_ling-1 if adopt_legal4==1
replace legal4_demo=legal4_demo-1 if adopt_legal4==1
replace legal4_bound=legal4_bound-1 if adopt_legal4==1

replace legal4_ling=legal4_ling/n_peers_ling
replace legal4_demo=legal4_demo/n_peers_demo
replace legal4_bound=legal4_bound/n_peers_bound

bysort year group_ling: egen legal6_ling=total(adopt_legal6)
bysort year group_demo: egen legal6_demo=total(adopt_legal6)
bysort year group_bound_aus: egen legal6_bound_aus= total(adopt_legal6)
bysort year group_bound_bel: egen legal6_bound_bel= total(adopt_legal6)
bysort year group_bound_bul: egen legal6_bound_bul= total(adopt_legal6)
bysort year group_bound_cro: egen legal6_bound_cro= total(adopt_legal6)
bysort year group_bound_cyp: egen legal6_bound_cyp= total(adopt_legal6)
bysort year group_bound_cze: egen legal6_bound_cze= total(adopt_legal6)
bysort year group_bound_den: egen legal6_bound_den= total(adopt_legal6)
bysort year group_bound_est: egen legal6_bound_est= total(adopt_legal6)
bysort year group_bound_fin: egen legal6_bound_fin= total(adopt_legal6)
bysort year group_bound_ger: egen legal6_bound_ger= total(adopt_legal6)
bysort year group_bound_gre: egen legal6_bound_gre= total(adopt_legal6)
bysort year group_bound_hun: egen legal6_bound_hun= total(adopt_legal6)
bysort year group_bound_ice: egen legal6_bound_ice= total(adopt_legal6)
bysort year group_bound_ita: egen legal6_bound_ita= total(adopt_legal6)
bysort year group_bound_lat: egen legal6_bound_lat= total(adopt_legal6)
bysort year group_bound_lit: egen legal6_bound_lit= total(adopt_legal6)
bysort year group_bound_lux: egen legal6_bound_lux= total(adopt_legal6)
bysort year group_bound_net: egen legal6_bound_net= total(adopt_legal6)
bysort year group_bound_nor: egen legal6_bound_nor= total(adopt_legal6)
bysort year group_bound_pol: egen legal6_bound_pol= total(adopt_legal6)
bysort year group_bound_por: egen legal6_bound_por= total(adopt_legal6)
bysort year group_bound_rom: egen legal6_bound_rom= total(adopt_legal6)
bysort year group_bound_slo: egen legal6_bound_slo= total(adopt_legal6)
bysort year group_bound_slove: egen legal6_bound_slove= total(adopt_legal6)
bysort year group_bound_spa: egen legal6_bound_spa= total(adopt_legal6)
bysort year group_bound_swe: egen legal6_bound_swe= total(adopt_legal6)
bysort year group_bound_swi: egen legal6_bound_swi= total(adopt_legal6)

gen legal6_bound=.
replace legal6_bound=legal6_bound_aus if country=="Austria"
replace legal6_bound=legal6_bound_bel if country=="Belgium"
replace legal6_bound=legal6_bound_bul if country=="Bulgaria"
replace legal6_bound=legal6_bound_cro if country=="Croatia"
replace legal6_bound=legal6_bound_cyp if country=="Cyprus"
replace legal6_bound=legal6_bound_cze if country=="Czech Republic"
replace legal6_bound=legal6_bound_den if country=="Denmark"
replace legal6_bound=legal6_bound_est if country=="Estonia"
replace legal6_bound=legal6_bound_fin if country=="Finland"
replace legal6_bound=legal6_bound_ger if country=="Germany"
replace legal6_bound=legal6_bound_gre if country=="Greece"
replace legal6_bound=legal6_bound_hun if country=="Hungary"
replace legal6_bound=legal6_bound_ice if country=="Iceland"
replace legal6_bound=legal6_bound_ita if country=="Italy"
replace legal6_bound=legal6_bound_lat if country=="Latvia"
replace legal6_bound=legal6_bound_lit if country=="Lithuania"
replace legal6_bound=legal6_bound_lux if country=="Luxembourg"
replace legal6_bound=legal6_bound_net if country=="Netherlands"
replace legal6_bound=legal6_bound_nor if country=="Norway"
replace legal6_bound=legal6_bound_pol if country=="Poland"
replace legal6_bound=legal6_bound_por if country=="Portugal"
replace legal6_bound=legal6_bound_rom if country=="Romania"
replace legal6_bound=legal6_bound_slo if country=="Slovakia"
replace legal6_bound=legal6_bound_slove if country=="Slovenia"
replace legal6_bound=legal6_bound_spa if country=="Spain"
replace legal6_bound=legal6_bound_swe if country=="Sweden"
replace legal6_bound=legal6_bound_swi if country=="Switzerland"

replace legal6_ling=legal6_ling-1 if adopt_legal6==1
replace legal6_demo=legal6_demo-1 if adopt_legal6==1
replace legal6_bound=legal6_bound-1 if adopt_legal6==1

replace legal6_ling=legal6_ling/n_peers_ling
replace legal6_demo=legal6_demo/n_peers_demo
replace legal6_bound=legal6_bound/n_peers_bound

* Variables: imit[time-span]_dm7_ling, imit[time-span]_dm7_demo, imit[time-span]_dm7_bound
* Description: Cumulative sum of the proportion of linguistic, historical and geographical peers that have have adopted ow district magnitude (with alternative cut offs 7-9) or high electoral threshold (with alternative cut-offs 4-6) within [time-span]

* 2 years
by country (year), sort: gen imit2_dm7_ling = sum(dm7_ling)-sum(dm7_ling[_n-2])
by country (year), sort: gen imit2_dm7_demo = sum(dm7_demo)-sum(dm7_demo[_n-2])
by country (year), sort: gen imit2_dm7_bound = sum(dm7_bound)-sum(dm7_bound[_n-2])

by country (year), sort: gen imit2_dm9_ling = sum(dm9_ling)-sum(dm9_ling[_n-2])
by country (year), sort: gen imit2_dm9_demo = sum(dm9_demo)-sum(dm9_demo[_n-2])
by country (year), sort: gen imit2_dm9_bound = sum(dm9_bound)-sum(dm9_bound[_n-2])

by country (year), sort: gen imit2_legal4_ling = sum(legal4_ling)-sum(legal4_ling[_n-2])
by country (year), sort: gen imit2_legal4_demo = sum(legal4_demo)-sum(legal4_demo[_n-2])
by country (year), sort: gen imit2_legal4_bound = sum(legal4_bound)-sum(legal4_bound[_n-2])

by country (year), sort: gen imit2_legal6_ling = sum(legal6_ling)-sum(legal6_ling[_n-2])
by country (year), sort: gen imit2_legal6_demo = sum(legal6_demo)-sum(legal6_demo[_n-2])
by country (year), sort: gen imit2_legal6_bound = sum(legal6_bound)-sum(legal6_bound[_n-2])

* Variables: lag_enpv, lag_enp_seat
* Description: Lagging covariates

sort country year 
by country: gen lag_enpv = enpv[_n-1]

sort country year 
by country: gen lag_enp_seat = enp_seat[_n-1]

**********
*Analyses*
**********

logit dv_dm7 imit2_dm7_bound lag_enpv lag_enp_seat if election_year==1 & number_dm7!=., cluster(country)
logit dv_dm7 imit2_dm7_ling lag_enpv lag_enp_seat if election_year==1 & number_dm7!=., cluster(country)
logit dv_dm7 imit2_dm7_demo lag_enpv lag_enp_seat if election_year==1 & number_dm7!=., cluster(country)

logit dv_dm9 imit2_dm9_bound lag_enpv lag_enp_seat if election_year==1 & number_dm9!=., cluster(country)
logit dv_dm9 imit2_dm9_ling lag_enpv lag_enp_seat if election_year==1 & number_dm9!=., cluster(country)
logit dv_dm9 imit2_dm9_demo lag_enpv lag_enp_seat if election_year==1 & number_dm9!=., cluster(country)

logit dv_legal4 imit2_legal4_bound lag_enpv lag_enp_seat if election_year==1 & number_legal4!=., cluster(country)
logit dv_legal4 imit2_legal4_ling lag_enpv lag_enp_seat if election_year==1 & number_legal4!=., cluster(country)
logit dv_legal4 imit2_legal4_demo lag_enpv lag_enp_seat if election_year==1 & number_legal4!=., cluster(country)

logit dv_legal6 imit2_legal6_bound lag_enpv lag_enp_seat if election_year==1 & number_legal6!=., cluster(country)
logit dv_legal6 imit2_legal6_ling lag_enpv lag_enp_seat if election_year==1 & number_legal6!=., cluster(country)
logit dv_legal6 imit2_legal6_demo lag_enpv lag_enp_seat if election_year==1 & number_legal6!=., cluster(country)
